<?php

namespace App\Http\Controllers\Index;


use App\Repositories\ArticleRepository;
use App\Repositories\SearchRepository;
use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{

    protected $article;
    protected $search;
    protected $user;


    public function __construct(ArticleRepository $article,SearchRepository $search,UserRepository $user)
    {
        $this->article=$article;
        $this->search=$search;
        $this->user=$user;

    }
    //


    /**
     * 前端首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //

        //浏览最多的三篇
        $count_hot=$this->article->getHot('3','count','1');

        //最新发布的四篇
        $time_hot=$this->article->getHot('4','created_at','1');

        //最新五篇公告
        $announce=$this->article->getHot('5','created_at','2');

        //重要信息
        $importantNews=$this->article->limitGetRecordByStatus('3','1','6','1');

        //政策四篇最新
        //人才
        $talentPolicy=$this->article->getHot('4','created_at','4');
        //产业
        $estatePolicy=$this->article->getHot('4','created_at','5');




        //各机构资讯展示
        $ouda=$this->article->limitGetRecordByStatus('1','1','4','14');

        $xianruo=$this->article->limitGetRecordByStatus('1','1','4','13');

        $cheku=$this->article->limitGetRecordByStatus('1','1','4','15');

        $wanxue=$this->article->limitGetRecordByStatus('1','1','4','16');

        $renren=$this->article->limitGetRecordByStatus('1','1','4','17');

        $smts=$this->article->limitGetRecordByStatus('1','1','4','18');


        $arr['renren']=$renren;
        $arr['smts']=$smts;
        $arr['ouda']=$ouda;
        $arr['xianruo']=$xianruo;
        $arr['cheku']=$cheku;
        $arr['wanxue']=$wanxue;
        $arr['importantNews']=$importantNews;
        $arr['countHot']=$count_hot;
        $arr['timeHot']=$time_hot;
        $arr['announce']=$announce;
        $arr['talentPolicy']=$talentPolicy;
        $arr['estatePolicy']=$estatePolicy;

        return view('index.index',['data_arr'=>$arr]);

    }


    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail(Request $request)
    {
        $article='';
        if($request->input('id')!=''){

            $this->article->incrementCount($request->input('id'));

            $article=$this->article->getById($request->input('id'));

        }else{

            return redirect('api/index');
        }

        return view('index.detail',['article'=>$article]);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function more(Request $request)
    {
        $user=$request->input('user');

        if($user!=''){
            $articles=$this->article->getArticleByUser($user,'1');
        }else{

            return redirect('');
        }


        return view('index.more',['articles'=>$articles]);
    }


    /**
     * 根据文章类别返回更多
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
     */
    public function category_more(Request $request)
    {
        $category=$request->input('user');

        if($category!=''){
            $articles=$this->article->getMoreByCategory($category);
        }else{

            return redirect('');
        }

        return view('index.more',['articles'=>$articles]);
    }

    /**
     * 搜索接口，返回列表页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function search(Request $request)
    {

        if($request->input('content')!=null){

            //判断之前是否有类似搜索关键词
            if($this->search->recordSearchKeyWord($request->input('content'))){

            }else{
                $this->search->store(['word'=>$request->input('content')])  ;
            }

            $articles=$this->article->searchReturn($request->input('content'));

            return view('index.more',['articles'=>$articles]);

        }else{

            //未输入内容不跳转页面
            return redirect('api/index');

        }

    }

}
